package com.imooc.rabbit.producer.broker;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @description: 同步线程池
 * @author: Yz
 * @create: 2020-10-10 11:20
 **/
@Slf4j
public class AsyncBaseQueue {

    /**
     * 线程池大小
     */
    private static final int THREAD_SIZE = Runtime.getRuntime().availableProcessors();

    /**
     * 队列大小
     */
    private static final int QUEUE_SIZE = 10000;

    private static ExecutorService senderAsync = new ThreadPoolExecutor(THREAD_SIZE, THREAD_SIZE,
            60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(QUEUE_SIZE),
            new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("rabbitmq_client_async_sender");
            return thread;
        }
    }, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            log.error("async sender is error rejected,runnable:{},executor:{}",r,executor);
        }
    });

    public static void submit(Runnable runnable){
        senderAsync.submit(runnable);
    }
}
